/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.app.server.processor;

import com.bugull.farm.core.cache.PropertiesCache;
import org.apache.logging.log4j.*;
import com.bugull.farm.app.server.cache.AppCache;
import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.Processor;
import com.bugull.redis.RedisConnection;
import com.bugull.redis.mq.MQClient;

import io.netty.channel.ChannelHandlerContext;

/**
 * Control command from app. Just publish message to device topic.
 * 
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class ControlProcessor implements Processor {
    
    private final static Logger logger = LogManager.getLogger(ControlProcessor.class);
    private PropertiesCache props = PropertiesCache.getInstance();

    @Override
    public void process(ChannelHandlerContext ctx, Frame frame) throws Exception {
        String mac = frame.getHexStr("mac");
        String seq = frame.getHexStr("serial");
        String cmd = frame.getHexStr("cmd");
        //record request to cache
        AppCache.getInstance().addRequest(mac + seq + cmd, ctx.channel());
        //publish to redis
        MQClient client = RedisConnection.getInstance().getMQClient();
        client.publish(props.get("redis.topic.dev"), frame.getRequest());
    }

}
